package com.oauth.google.server.controller;

import java.util.List;

import org.gwtwidgets.server.spring.GWTSpringController;
import org.springframework.beans.factory.annotation.Autowired;

import com.oauth.common.server.util.OAuthRequestUtil;
import com.oauth.google.client.beans.youtube.GoogleYouTubeModel;
import com.oauth.google.client.beans.youtube.YouTubeVideo;
import com.oauth.google.client.exception.GoogleOAuthException;
import com.oauth.google.client.rpc.youtube.YouTubeRemoteService;
import com.oauth.google.server.service.YoutubeOAuthService;
import com.oauth.logging.OAuthLogger;

public class GoogleYouTubeController extends GWTSpringController implements YouTubeRemoteService {

	private static final long serialVersionUID = -5758464341967618224L;

	@Autowired
	private YoutubeOAuthService mYoutubeOAuthService;
	private OAuthLogger logger = OAuthLogger.getLogger(this.getClass());

	/**
	 * @param pYoutubeOAuthService
	 *            the youtubeOAuthService to set
	 */
	public void setYoutubeOAuthService(YoutubeOAuthService pYoutubeOAuthService) {
		mYoutubeOAuthService = pYoutubeOAuthService;
	}

	@Override
	public GoogleYouTubeModel getYouTubeVideos() throws GoogleOAuthException {
		GoogleYouTubeModel model = new GoogleYouTubeModel();
		Long memberId = OAuthRequestUtil.getMemberId(getThreadLocalRequest());

		try {
			List<YouTubeVideo> videos = mYoutubeOAuthService.getYoutubeVideos(memberId);
			model.setVideos(videos);
			return model;
		} catch (Exception e) {
			logger.error("Error in fetching youtube videos", e);
			throw new GoogleOAuthException();
		}
	}

}
